<?php
/**
* @file Usereventlogm.php
* @synopsis  用户行为日志
* @author Yee, <rlk002@gmail.com>
* @version 1.0
* @date 2015年8月17日14:53:13
*/

class Usereventlogm extends CI_Model
{
	const USEREVENTACTIONTYPE_POST = 1;				// 发帖帖子
	const USEREVENTACTIONTYPE_COMMENT = 2;			// 评论
	const USEREVENTACTIONTYPE_FAVORITE = 3;			// 收藏
	const USEREVENTACTIONTYPE_FOLLOW = 4;			// 关注
	const USEREVENTACTIONTYPE_DAREN = 5;			// 达人列表
	const USEREVENTACTIONTYPE_FOLLOW_UN = 6;		// 取消关注
	const USEREVENTACTIONTYPE_ADDCONTENT = 7;		// 发布新动态
//	const USEREVENTACTIONTYPE_ADDSHARE = 8;		    // 发布分享
//	const USEREVENTACTIONTYPE_ADDHELP = 9;		    // 发布求助
//	const USEREVENTACTIONTYPE_COMMENTSHARE = 10;    // 评论分享
//	const USEREVENTACTIONTYPE_COMMENTHELP = 11;        // 评论求助
//	const USEREVENTACTIONTYPE_FAVORITE_SHARE = 12;    // 收藏分享
//	const USEREVENTACTIONTYPE_FAVORITE_HELP = 13;    // 收藏求助


	const USEREVENTACTIONTYPE_ADD_VOTE     		= 1001;		// 发起了投票
	const USEREVENTACTIONTYPE_COMMENT_VOTE 		= 1002;		// 评论了投票
	const USEREVENTACTIONTYPE_PARTAKE_VOTE 		= 1003;		// 参与了投票
	const USEREVENTACTIONTYPE_ADD_QUESTION 		= 1004;		// 发起了问题
	const USEREVENTACTIONTYPE_COMMENT_QUESTION 	= 1005;		// 评论了问题
	const USEREVENTACTIONTYPE_FAVORITE_QUESTION = 1006;     // 收藏了问题
	const USEREVENTACTIONTYPE_FAVORITE_VOTE 	= 1007;     // 收藏了投票


	// 类型 病历
	const USEREVENTTYPE_MEDICAL_RECORD = 1; // 类型 病历
	const USEREVENTTYPE_QUESTION_VOTE  = 2; // 类型 问答
	

    function __construct()
	{
		parent::__construct();
		$this->load->model('commentm');
		$this->load->model('userm');
		$this->load->model('feedm');
		$this->load->model('postm');
        $this->load->model("praisem");
		$this->load->model("question_votem");
		$this->load->model("fellow_follow_model");
		$this->load->model("followm");
	}
    //用户发布动态计入记录
    public function insert_user_event($user_id,$feed_id)
    {
        $query = array("action"=>"add_content","action_type"=>self::USEREVENTACTIONTYPE_ADDCONTENT,"feed_id"=>$feed_id,"uid1"=>$user_id,"addtime"=>time());
        return $this->insert_user_event_log($query);
    }
	//更新用户行为日志
	public function insert_user_event_log($event_log_array)
	{
		$return = $this->db->insert("ml_user_event_log",$event_log_array);
		if ($return) {
			$type = isset($event_log_array['type']) ? $event_log_array['type'] : 1;
			$this->set_unread_notice($event_log_array['uid1'], $type);
		}
		return $return;
	}

	/**
	 * 设置未读动态提示
	 * @param int $user_id
	 * @return bool
	 */
	public function set_unread_notice($user_id = 0, $notice_type = 1)
	{
		if ($user_id == 31495) return false; // 屏蔽官方帐号

		$fans_user_ids = $this->followm->get_fans_ids($user_id);
		if (!$fans_user_ids) return false;

		foreach($fans_user_ids as $v) {
			$key = USER_INFO_PREFIX.$v;
			if ($notice_type == 1) {
				$this->redis->hSet($key, 'medical_status', 1);
			} else {
				$this->redis->hSet($key, 'question_vote_status', 1);
			}
			$this->redis->expire($key, USER_INFO_KEY_EXPIRE_TIME);
		}
		return true;
	}

	/**
	 * 获取用户行为日志  1是发帖 2是评论 3是收藏  4是关注 5是达人计划
	 * @param $user_id
	 * @param $page
	 * @param $page_size
	 * @return array
	 */
	public function get_user_event_log($user_id, $page, $page_size)
	{
		$start = ($page - 1) * $page_size;
		$limit = ($start ? $start . ', ' : '') . $page_size;

		$user_ids = $this->followm->get_follow_ids($user_id);
		if (!$user_ids) return array();

		//去掉数组里的空值，去掉官方微博
		foreach ($user_ids as $key => $value) {
			if (!$value || $value == 31495 || $value == $user_id) {
				unset($user_ids[$key]);
			}
		}

		$user_ids = array_values($user_ids);
		if (!$user_ids) return array();

		$filter = array('a.type' => 1);

		// 2.0.0版本添加自身的动态
		if (version_compare($this->softversion, '2.0.0') >= 0) {
			$user_ids[] = $user_id;
			$user_event_log_result = $this->db->select('a.*')
					->from('ml_user_event_log as a')
					->where($filter)
					->group_start()
					->where('b.is_show', 1)
					->or_where('a.post_id', 0)
					->group_end()
					->group_start()
					->where('c.is_show', 1)
					->or_where('a.comment_id', 0)
					->group_end()
					->group_start()
					->where('d.is_show', 1)
					->or_where('a.feed_id', 0)
					->group_end()
					->where('a.type', self::USEREVENTTYPE_MEDICAL_RECORD)
					->where_in('b.post_type_extend', array(1,2,3))
					->where_in('a.uid1', $user_ids)
					->join('ml_post as b', 'a.post_id = b.id', 'left')
					->join('ml_comment as c', 'a.comment_id = c.id', 'left')
					->join('ml_feed as d', 'a.feed_id = d.id', 'left')
					->order_by('a.id', 'DESC')
					->limit($page_size, $start)
					->get()
					->result_array();

		} else {

			$user_event_log_result = $this->db->select('a.*')
					->from('ml_user_event_log as a')
					->where($filter)
					->group_start()
					->where('b.is_show', 1)
					->or_where('a.post_id', 0)
					->group_end()
					->group_start()
					->where('c.is_show', 1)
					->or_where('a.comment_id', 0)
					->group_end()
					->where('a.action_type <', 5)
					->where('a.type', self::USEREVENTTYPE_MEDICAL_RECORD)
					->where_in('b.post_type_extend', array(1,2,3))
					->where_in('a.uid1', $user_ids)
					->join('ml_post as b', 'a.post_id = b.id', 'left')
					->join('ml_comment as c', 'a.comment_id = c.id', 'left')
					->order_by('a.id', 'DESC')
					->limit($page_size, $start)
					->get()
					->result_array();
		}

		if (!$user_event_log_result) return array();

		// 加载用户 event 列表
		list($user_event_log_return_array, $event_ids) = $this->get_user_event_by_log_results($user_event_log_result, $user_id);

		// 加载点赞列表
		$praise_lst = $this->praisem->get_praise_lst_by_event_ids($event_ids);

		// 加载评论列表
		$comment_lst = $this->get_comment_lst_by_event_ids($event_ids);

		// 加载点赞和评论进结果列表
		foreach ($user_event_log_return_array as &$item) {
			$event_id = $item['id'];
			if (isset($comment_lst[$event_id])) {
				$item['event_comment_lst'] = $comment_lst[$event_id];
			} else {
				$item['event_comment_lst'] = array();
			}
			if (isset($praise_lst[$event_id])) {
				$item['praise_lst'] = $praise_lst[$event_id];
			} else {
				$item['praise_lst'] = array();
			}
		}

		return $user_event_log_return_array;
	}

	/**
	 * 中间态函数, 获取动态的详情
	 * @param $user_event_log_result
	 * @param $user_id : 如果是自身状态,自己的 user_id
	 * @return array
	 */
	private function get_user_event_by_log_results($user_event_log_result, $user_id = '')
	{
		$user_event_log_return_array = [];
		$last_event = [];

		$event_ids = [];

		foreach ($user_event_log_result as $value) {

			// user_id: 看首页feed列表 && 自身动态 && 非微博型
			if ($user_id && $value["uid1"] == $user_id && $value['action_type'] != self::USEREVENTACTIONTYPE_ADDCONTENT && (version_compare($this->softversion, '2.2.0') < 0)) {
				continue;
			}

			$event_ids[] = $value['id'];

			switch ($value['action_type']) {
				case self::USEREVENTACTIONTYPE_POST: // 发帖帖子
					$user_event_log_return_array[] = $this->get_user_insert_post($value);
					break;
				case self::USEREVENTACTIONTYPE_COMMENT: // 评论
					$user_event_log_return_array[] = $this->get_user_comment_post($value);
					break;
				case self::USEREVENTACTIONTYPE_FAVORITE: // 收藏
					$user_event_log_return_array[] = $this->get_user_favorite_post($value);
					break;
				case self::USEREVENTACTIONTYPE_FOLLOW: // 关注
					$one_event = $this->get_user_fellow($value);
					if ($last_event) {
						if ($last_event['uid1'] == $one_event['uid1'] && $last_event['uid2'] == $one_event['uid2']) {
							break;
						}
					}
					$user_event_log_return_array[] = $one_event;
					$last_event = $one_event;
					break;
				case self::USEREVENTACTIONTYPE_ADDCONTENT: // 发布新动态
					$user_event_log_return_array[] = $this->get_user_feed($value);
					break;

				case self::USEREVENTACTIONTYPE_ADD_VOTE:// 发起了投票
					$user_event_log_return_array[] = $this->get_vote_info($value);
					break;

				case self::USEREVENTACTIONTYPE_COMMENT_VOTE:// 评论了投票
					$user_event_log_return_array[] = $this->get_vote_comment_info($value);
					break;

				case self::USEREVENTACTIONTYPE_PARTAKE_VOTE:// 参与了投票
					$user_event_log_return_array[] = $this->get_vote_relation_info($value);
					break;

				case self::USEREVENTACTIONTYPE_ADD_QUESTION:// 发起了问题
					$user_event_log_return_array[] = $this->get_question_info($value);
					break;

				case self::USEREVENTACTIONTYPE_COMMENT_QUESTION:        // 评论了问题
					$user_event_log_return_array[] = $this->get_question_comment_info($value);
					break;
				case self::USEREVENTACTIONTYPE_FAVORITE_QUESTION:        // 收藏了问题
					$user_event_log_return_array[] = $this->get_favorite_question_info($value);
					break;
				case self::USEREVENTACTIONTYPE_FAVORITE_VOTE:            // 收藏了投票
					$user_event_log_return_array[] = $this->get_favorite_vote_info($value);
					break;
//				case self::USEREVENTACTIONTYPE_ADDSHARE:                // 发布了分享
//					$one_event = $this->get_add_share_info($value);
//					$user_event_log_return_array[] = $one_event;
//					break;
//				case self::USEREVENTACTIONTYPE_ADDHELP:                    // 发布了求助
//					$one_event = $this->get_question_comment_info($value);
//					$user_event_log_return_array[] = $one_event;
//					break;

				default:
					# code...
					break;
			}
		}

		return array($user_event_log_return_array, $event_ids);
	}

	public function get_user_insert_post($insert_post_log)
	{
		$userinfo1 = $this->userm->getUserInfo($insert_post_log['uid1']);
		$post_info = $this->postm->get_one_post_4_search($insert_post_log["post_id"], $this->uid);

		$return = array();
		$return['message'] = "发布了一张帖子";
		$return['uid1'] = $insert_post_log['uid1'];
		$return['uid1_username'] = $userinfo1['username'];
		$return['uid1_head_ico'] = $userinfo1['head_ico'];
		$return['uid1_thread_name'] = $userinfo1['thread_name'];
		$return['uid1_practice_hospital'] = $userinfo1['practice_hospital'];
		$return['uid1_is_expert'] = $userinfo1['is_expert'];
		$return['uid1_verified_status'] = $userinfo1['verified_status'];
		$return['uid1_admin_level'] = $userinfo1['admin_level'];

		$return['post_id'] = $insert_post_log["post_id"];
		$return['post_message'] = $post_info['subject'] ? $post_info['subject'] : $post_info['message'];
		$return['post_imgs'] = $post_info['post_imgs'];
		$return['author_name'] = $post_info['author_name'];
		$return['author_id'] = $post_info['author_id'];
		$return['thread_name'] = $post_info['thread_name'];
		$return['head_ico'] = $post_info['head_ico'];
		$return['admin_level'] = $post_info['admin_level'];
		$return['is_expert'] = $post_info['is_expert'];
		$return['verified_status'] = $post_info['verified_status'];
		$return['aspect_ratio'] = $post_info['aspect_ratio'];
		$return['action_time'] = $insert_post_log['addtime'];
		$return['action_type'] = $insert_post_log['action_type'];
		$return['favorite_status'] = $post_info['favorite_status'];
		$return['id'] = $insert_post_log['id'];
		$return['post_type_extend'] = $post_info['post_type_extend'];

		return $return;
	}

	//获取他人行为日志

	public function get_user_comment_post($insert_post_log)
	{
		$userinfo1 = $this->userm->getUserInfo($insert_post_log['uid1']);
		$post_info = $this->postm->get_one_post_4_search($insert_post_log["post_id"], $this->uid);
		$comment_message = $this->commentm->get_comment_message($insert_post_log["comment_id"]);

		$return = array();
		$return['uid1'] = $insert_post_log['uid1'];
		$return['message'] = "评论帖子:" . $comment_message;
		$return['uid1_username'] = $userinfo1['username'];
		$return['uid1_head_ico'] = $userinfo1['head_ico'];
		$return['uid1_thread_name'] = $userinfo1['thread_name'];
		$return['uid1_practice_hospital'] = $userinfo1['practice_hospital'];
		$return['uid1_is_expert'] = $userinfo1['is_expert'];
		$return['uid1_verified_status'] = $userinfo1['verified_status'];
		$return['uid1_admin_level'] = $userinfo1['admin_level'];

		$return['post_id'] = $insert_post_log["post_id"];
		$return['post_message'] = $post_info['subject'] ? $post_info['subject'] : $post_info['message'];
		$return['post_imgs'] = $post_info['post_imgs'];
		$return['author_name'] = $post_info['author_name'];
		$return['author_id'] = $post_info['author_id'];
		$return['thread_name'] = $post_info['thread_name'];
		$return['head_ico'] = $post_info['head_ico'];
		$return['admin_level'] = $post_info['admin_level'];
		$return['is_expert'] = $post_info['is_expert'];
		$return['verified_status'] = $post_info['verified_status'];
		$return['aspect_ratio'] = $post_info['aspect_ratio'];
		$return['comment_id'] = $insert_post_log["comment_id"];
		$return['comment_message'] = $comment_message;
		$return['action_time'] = $insert_post_log['addtime'];
		$return['action_type'] = $insert_post_log['action_type'];
		$return['favorite_status'] = $post_info['favorite_status'];
		$return['id'] = $insert_post_log['id'];
		$return['post_type_extend'] = $post_info['post_type_extend'];

		return $return;
	}

	//获取发帖信息

	public function get_user_favorite_post($insert_post_log)
	{
		$userinfo1 = $this->userm->getUserInfo($insert_post_log['uid1']);
		$post_info = $this->postm->get_one_post_4_search($insert_post_log["post_id"],$this->uid);

		$return = array();
		$return['uid1'] = $insert_post_log['uid1'];
		$return['message'] = "收藏了一张帖子";
		$return['uid1_username'] = $userinfo1['username'];
		$return['uid1_head_ico'] = $userinfo1['head_ico'];
		$return['uid1_thread_name'] = $userinfo1['thread_name'];
		$return['uid1_practice_hospital'] = $userinfo1['practice_hospital'];
		$return['uid1_is_expert'] = $userinfo1['is_expert'];
		$return['uid1_verified_status'] = $userinfo1['verified_status'];
		$return['uid1_admin_level'] = $userinfo1['admin_level'];


		$return['post_id'] = $insert_post_log["post_id"];
		$return['post_message'] = $post_info['subject'] ? $post_info['subject'] : $post_info['message'];
		$return['post_imgs'] = $post_info['post_imgs'];
		$return['author_name'] = $post_info['author_name'];
		$return['author_id'] = $post_info['author_id'];
		$return['thread_name'] = $post_info['thread_name'];
		$return['head_ico'] = $post_info['head_ico'];
		$return['admin_level'] = $post_info['admin_level'];
		$return['is_expert'] = $post_info['is_expert'];
		$return['verified_status'] = $post_info['verified_status'];
		$return['aspect_ratio'] = $post_info['aspect_ratio'];
		$return['action_time'] = $insert_post_log['addtime'];
		$return['action_type'] = $insert_post_log['action_type'];
		$return['favorite_status'] = $post_info['favorite_status'];
		$return['id'] = $insert_post_log['id'];
		$return['post_type_extend'] = $post_info['post_type_extend'];


		return $return;
	}

	public function get_user_fellow($insert_post_log)
	{
		$userinfo1 = $this->userm->get_one_user_4_search($insert_post_log['uid1'], $this->uid);
		$userinfo2 = $this->userm->get_one_user_4_search($insert_post_log['uid2'], $this->uid);

		$return = array();
		$return['uid1'] = $insert_post_log['uid1'];
		$return['uid2'] = $insert_post_log['uid2'];
		$return['message'] = $userinfo1['username'] . " 关注了 " . $userinfo2['username'];
		$return['uid1_username'] = $userinfo1['username'];
		$return['uid1_head_ico'] = $userinfo1['head_ico'];
		$return['uid1_thread_name'] = $userinfo1['specialty_name'];
		$return['uid1_practice_hospital'] = $userinfo1['practice_hospital'];
		$return['uid1_is_expert'] = $userinfo1['is_expert'];
		$return['uid1_verified_status'] = $userinfo1['verified_status'];
		$return['uid1_admin_level'] = $userinfo1['admin_level'];


		$return['uid2_username'] = $userinfo2['username'];
		$return['uid2_head_ico'] = $userinfo2['head_ico'];
		$return['uid2_thread_name'] = $userinfo2['specialty_name'];
		$return['uid2_practice_hospital'] = $userinfo2['practice_hospital'];

		$return['uid2_verified_status'] = $userinfo2['verified_status'];
		$return['uid2_admin_level'] = $userinfo2['admin_level'];
		$return['uid2_is_expert'] = $userinfo2['is_expert'];
		$return['action_time'] = $insert_post_log['addtime'];
		$return['action_type'] = $insert_post_log['action_type'];
		$return['follow_status'] = $userinfo2['follow_status'];
		$return['id'] = $insert_post_log['id'];

		return $return;
	}

	public function get_user_feed($insert_post_log)
	{
		$userinfo1 = $this->userm->getUserInfo($insert_post_log['uid1']);
		$feed_info = $this->feedm->one_feed_content($insert_post_log["feed_id"]);

		$return = array();
		$return['uid1'] = $insert_post_log['uid1'];
		$return['uid1_username'] = $userinfo1['username'];
		$return['uid1_head_ico'] = $userinfo1['head_ico'];
		$return['uid1_thread_name'] = $userinfo1['thread_name'];
		$return['uid1_practice_hospital'] = $userinfo1['practice_hospital'];
		$return['uid1_is_expert'] = $userinfo1['is_expert'];
		$return['uid1_verified_status'] = $userinfo1['verified_status'];
		$return['uid1_admin_level'] = $userinfo1['admin_level'];


		$return['feed_id'] = $feed_info["id"];
		$return['message'] = $feed_info["content"];

		$return['action_type'] = $insert_post_log['action_type'];
		$return['action_time'] = $feed_info['create_time'];

		$return['id'] = $insert_post_log['id'];
//        $praise_uids = $this->praisem->praise_list($return['id']);
//        $return['priase_list'] = $this->praise_user_list($praise_uids);
//
//        $comment_list_array = $this->commentm->get_comment_list_1_9_0($return['id'],0,100,'asc',1);
//        $return['comment_list'] = $comment_list_array;

		return $return;
	}

	/**
	 * 获取投票信息
	 * @param $insert_post_log
	 * @return array
	 */
	public function get_vote_info($insert_post_log)
	{
		$userinfo1 = $this->userm->getUserInfo($insert_post_log['uid1']);
		$post_info = $this->postm->get_one_post_4_search($insert_post_log["post_id"],$this->uid);

		$return = array();
		$return['message'] = "发起了一个投票";
		$return['uid1'] = $insert_post_log['uid1'];
		$return['uid1_username'] = $userinfo1['username'];
		$return['uid1_head_ico'] = $userinfo1['head_ico'];
		$return['uid1_thread_name'] = $userinfo1['thread_name'];
		$return['uid1_practice_hospital'] = $userinfo1['practice_hospital'];
		$return['uid1_is_expert'] = $userinfo1['is_expert'];
		$return['uid1_verified_status'] = $userinfo1['verified_status'];
		$return['uid1_admin_level'] = $userinfo1['admin_level'];

		$return['post_id'] = $insert_post_log["post_id"];
		$return['post_message'] = $post_info['subject'] ? $post_info['subject'] : $post_info['message'];
		$return['post_imgs'] = $post_info['post_imgs'];
		$return['author_name'] = $post_info['author_name'];
		$return['author_id'] = $post_info['author_id'];
		$return['thread_name'] = $post_info['thread_name'];
		$return['head_ico'] = $post_info['head_ico'];
		$return['admin_level'] = $post_info['admin_level'];
		$return['is_expert'] = $post_info['is_expert'];
		$return['verified_status'] = $post_info['verified_status'];
		$return['aspect_ratio'] = $post_info['aspect_ratio'];
		$return['action_time'] = $insert_post_log['addtime'];
		$return['action_type'] = $insert_post_log['action_type'];
		$return['id'] = $insert_post_log['id'];
		$return['post_type_extend'] = $post_info['post_type_extend'];
		return $return;
	}

	/**
	 * 获取投票 评论 信息
	 * @param $insert_post_log
	 * @return array
	 */
	public function get_vote_comment_info($insert_post_log)
	{
		$userinfo1 = $this->userm->getUserInfo($insert_post_log['uid1']);
		$post_info = $this->postm->get_one_post_4_search($insert_post_log["post_id"],$this->uid);
		$comment_message = $this->commentm->get_comment_message($insert_post_log["comment_id"]);
		$return = array();
		$return['uid1'] = $insert_post_log['uid1'];
		$return['message'] = "评论投票: " . $comment_message;
		$return['uid1_username'] = $userinfo1['username'];
		$return['uid1_head_ico'] = $userinfo1['head_ico'];
		$return['uid1_thread_name'] = $userinfo1['thread_name'];
		$return['uid1_practice_hospital'] = $userinfo1['practice_hospital'];
		$return['uid1_is_expert'] = $userinfo1['is_expert'];
		$return['uid1_verified_status'] = $userinfo1['verified_status'];
		$return['uid1_admin_level'] = $userinfo1['admin_level'];

		$return['post_id'] = $insert_post_log["post_id"];
		$return['post_message'] = $post_info['subject'] ? $post_info['subject'] : $post_info['message'];
		$return['post_imgs'] = $post_info['post_imgs'];
		$return['author_name'] = $post_info['author_name'];
		$return['author_id'] = $post_info['author_id'];
		$return['thread_name'] = $post_info['thread_name'];
		$return['head_ico'] = $post_info['head_ico'];
		$return['admin_level'] = $post_info['admin_level'];
		$return['is_expert'] = $post_info['is_expert'];
		$return['verified_status'] = $post_info['verified_status'];
		$return['aspect_ratio'] = $post_info['aspect_ratio'];
		$return['comment_id'] = $insert_post_log["comment_id"];
		$return['comment_message'] = $comment_message;
		$return['action_time'] = $insert_post_log['addtime'];
		$return['action_type'] = $insert_post_log['action_type'];
		$return['id'] = $insert_post_log['id'];
		$return['post_type_extend'] = $post_info['post_type_extend'];
		return $return;
	}

	/**
	 * 获取 参与投票 信息
	 * @param $insert_post_log
	 * @return array
	 */
	public function get_vote_relation_info($insert_post_log)
	{
		$userinfo1 = $this->userm->getUserInfo($insert_post_log['uid1']);
		$post_info = $this->postm->get_one_post_4_search($insert_post_log["post_id"],$this->uid);
		$vote_option = $this->question_votem->get_select_vote_option_name($insert_post_log['uid1'], $post_info['id']);
		$return = array();
		$return['uid1'] = $insert_post_log['uid1'];
		$return['message'] = "参与投票 ";
		$return['uid1_username'] = $userinfo1['username'];
		$return['uid1_head_ico'] = $userinfo1['head_ico'];
		$return['uid1_thread_name'] = $userinfo1['thread_name'];
		$return['uid1_practice_hospital'] = $userinfo1['practice_hospital'];
		$return['uid1_is_expert'] = $userinfo1['is_expert'];
		$return['uid1_verified_status'] = $userinfo1['verified_status'];
		$return['uid1_admin_level'] = $userinfo1['admin_level'];

		$return['post_id'] = $insert_post_log["post_id"];
		$return['post_message'] = $post_info['subject'] ? $post_info['subject'] : $post_info['message'];
		$return['post_imgs'] = $post_info['post_imgs'];
		$return['author_name'] = $post_info['author_name'];
		$return['author_id'] = $post_info['author_id'];
		$return['thread_name'] = $post_info['thread_name'];
		$return['head_ico'] = $post_info['head_ico'];
		$return['admin_level'] = $post_info['admin_level'];
		$return['is_expert'] = $post_info['is_expert'];
		$return['verified_status'] = $post_info['verified_status'];
		$return['aspect_ratio'] = $post_info['aspect_ratio'];
		$return['action_time'] = $insert_post_log['addtime'];
		$return['action_type'] = $insert_post_log['action_type'];
		$return['id'] = $insert_post_log['id'];
		$return['post_type_extend'] = $post_info['post_type_extend'];
		return $return;
	}


	//获取评论信息

	/**
	 * 获取 发布问题 信息
	 * @param $insert_post_log
	 * @return array
	 */
	public function get_question_info($insert_post_log)
	{
		$userinfo1 = $this->userm->getUserInfo($insert_post_log['uid1']);
		$post_info = $this->postm->get_one_post_4_search($insert_post_log["post_id"],$this->uid);
		$comment_message = $this->commentm->get_comment_message($insert_post_log["comment_id"]);
		$return = array();
		$return['uid1'] = $insert_post_log['uid1'];
		$return['message'] = "发布了一个提问 ";
		$return['uid1_username'] = $userinfo1['username'];
		$return['uid1_head_ico'] = $userinfo1['head_ico'];
		$return['uid1_thread_name'] = $userinfo1['thread_name'];
		$return['uid1_practice_hospital'] = $userinfo1['practice_hospital'];
		$return['uid1_is_expert'] = $userinfo1['is_expert'];
		$return['uid1_verified_status'] = $userinfo1['verified_status'];
		$return['uid1_admin_level'] = $userinfo1['admin_level'];

		$return['post_id'] = $insert_post_log["post_id"];
		$return['post_message'] = $post_info['subject'] ? $post_info['subject'] : $post_info['message'];
		$return['post_imgs'] = $post_info['post_imgs'];
		$return['author_name'] = $post_info['author_name'];
		$return['author_id'] = $post_info['author_id'];
		$return['thread_name'] = $post_info['thread_name'];
		$return['head_ico'] = $post_info['head_ico'];
		$return['admin_level'] = $post_info['admin_level'];
		$return['is_expert'] = $post_info['is_expert'];
		$return['verified_status'] = $post_info['verified_status'];
		$return['aspect_ratio'] = $post_info['aspect_ratio'];
		$return['comment_id'] = $insert_post_log["comment_id"];
		$return['comment_message'] = $comment_message;
		$return['action_time'] = $insert_post_log['addtime'];
		$return['action_type'] = $insert_post_log['action_type'];
		$return['id'] = $insert_post_log['id'];
		$return['post_type_extend'] = $post_info['post_type_extend'];
		return $return;
	}

	//获取收藏信息

	/**
	 * 获取 问题评论 信息
	 * @param $insert_post_log
	 * @return array
	 */
	public function get_question_comment_info($insert_post_log)
	{
		$userinfo1 = $this->userm->getUserInfo($insert_post_log['uid1']);
		$post_info = $this->postm->get_one_post_4_search($insert_post_log["post_id"],$this->uid);
		$comment_message = $this->commentm->get_comment_message($insert_post_log["comment_id"]);
		$return = array();
		$return['uid1'] = $insert_post_log['uid1'];
		$return['message'] = "评论提问:" . $comment_message;
		$return['uid1_username'] = $userinfo1['username'];
		$return['uid1_head_ico'] = $userinfo1['head_ico'];
		$return['uid1_thread_name'] = $userinfo1['thread_name'];
		$return['uid1_practice_hospital'] = $userinfo1['practice_hospital'];
		$return['uid1_is_expert'] = $userinfo1['is_expert'];
		$return['uid1_verified_status'] = $userinfo1['verified_status'];
		$return['uid1_admin_level'] = $userinfo1['admin_level'];

		$return['post_id'] = $insert_post_log["post_id"];
		$return['post_message'] = $post_info['subject'] ? $post_info['subject'] : $post_info['message'];
		$return['post_imgs'] = $post_info['post_imgs'];
		$return['author_name'] = $post_info['author_name'];
		$return['author_id'] = $post_info['author_id'];
		$return['thread_name'] = $post_info['thread_name'];
		$return['head_ico'] = $post_info['head_ico'];
		$return['admin_level'] = $post_info['admin_level'];
		$return['is_expert'] = $post_info['is_expert'];
		$return['verified_status'] = $post_info['verified_status'];
		$return['aspect_ratio'] = $post_info['aspect_ratio'];
		$return['comment_id'] = $insert_post_log["comment_id"];
		$return['comment_message'] = $comment_message;
		$return['action_time'] = $insert_post_log['addtime'];
		$return['action_type'] = $insert_post_log['action_type'];
		$return['favorite_status'] = $post_info['favorite_status'];
		$return['id'] = $insert_post_log['id'];
		$return['post_type_extend'] = $post_info['post_type_extend'];
		return $return;
	}

    //获取动态信息

	/**
	 * 获取 收藏了投票 信息
	 * @param $insert_post_log
	 * @return array
	 */
	public function get_favorite_vote_info($insert_post_log)
	{
		$userinfo1 = $this->userm->getUserInfo($insert_post_log['uid1']);
		$post_info = $this->postm->get_one_post_4_search($insert_post_log["post_id"], $this->uid);
		$return = array();
		$return['uid1'] = $insert_post_log['uid1'];
		$return['message'] = "收藏了一个投票";
		$return['uid1_username'] = $userinfo1['username'];
		$return['uid1_head_ico'] = $userinfo1['head_ico'];
		$return['uid1_thread_name'] = $userinfo1['thread_name'];
		$return['uid1_practice_hospital'] = $userinfo1['practice_hospital'];
		$return['uid1_is_expert'] = $userinfo1['is_expert'];
		$return['uid1_verified_status'] = $userinfo1['verified_status'];
		$return['uid1_admin_level'] = $userinfo1['admin_level'];

		$return['post_id'] = $insert_post_log["post_id"];
		$return['post_message'] = $post_info['subject'] ? $post_info['subject'] : $post_info['message'];
		$return['post_imgs'] = $post_info['post_imgs'];
		$return['author_name'] = $post_info['author_name'];
		$return['author_id'] = $post_info['author_id'];
		$return['thread_name'] = $post_info['thread_name'];
		$return['head_ico'] = $post_info['head_ico'];
		$return['admin_level'] = $post_info['admin_level'];
		$return['is_expert'] = $post_info['is_expert'];
		$return['verified_status'] = $post_info['verified_status'];
		$return['aspect_ratio'] = $post_info['aspect_ratio'];
		$return['comment_id'] = $insert_post_log["comment_id"];
		$return['comment_message'] = '';
		$return['action_time'] = $insert_post_log['addtime'];
		$return['action_type'] = $insert_post_log['action_type'];
		$return['favorite_status'] = $post_info['favorite_status'];
		$return['id'] = $insert_post_log['id'];
		$return['post_type_extend'] = $post_info['post_type_extend'];
		return $return;
	}

	/**
	 * 获取 收藏了问题 信息
	 * @param $insert_post_log
	 * @return array
	 */
	public function get_favorite_question_info($insert_post_log)
	{
		$userinfo1 = $this->userm->getUserInfo($insert_post_log['uid1']);
		$post_info = $this->postm->get_one_post_4_search($insert_post_log["post_id"],$this->uid);
		$return = array();
		$return['uid1'] = $insert_post_log['uid1'];
		$return['message'] = "收藏了一个问答";
		$return['uid1_username'] = $userinfo1['username'];
		$return['uid1_head_ico'] = $userinfo1['head_ico'];
		$return['uid1_thread_name'] = $userinfo1['thread_name'];
		$return['uid1_practice_hospital'] = $userinfo1['practice_hospital'];
		$return['uid1_is_expert'] = $userinfo1['is_expert'];
		$return['uid1_verified_status'] = $userinfo1['verified_status'];
		$return['uid1_admin_level'] = $userinfo1['admin_level'];

		$return['post_id'] = $insert_post_log["post_id"];
		$return['post_message'] = $post_info['subject'] ? $post_info['subject'] : $post_info['message'];
		$return['post_imgs'] = $post_info['post_imgs'];
		$return['author_name'] = $post_info['author_name'];
		$return['author_id'] = $post_info['author_id'];
		$return['thread_name'] = $post_info['thread_name'];
		$return['head_ico'] = $post_info['head_ico'];
		$return['admin_level'] = $post_info['admin_level'];
		$return['is_expert'] = $post_info['is_expert'];
		$return['verified_status'] = $post_info['verified_status'];
		$return['aspect_ratio'] = $post_info['aspect_ratio'];
		$return['comment_id'] = $insert_post_log["comment_id"];
		$return['comment_message'] = '';
		$return['action_time'] = $insert_post_log['addtime'];
		$return['action_type'] = $insert_post_log['action_type'];
		$return['favorite_status'] = $post_info['favorite_status'];
		$return['id'] = $insert_post_log['id'];
		$return['post_type_extend'] = $post_info['post_type_extend'];
		return $return;
	}

	/**
	 * 获取 发布分享 信息
	 * @param $insert_post_log
	 * @return array
	 */
	public function get_add_share_info($insert_post_log)
	{
		$userinfo1 = $this->userm->getUserInfo($insert_post_log['uid1']);
		$post_info = $this->postm->get_one_post_4_search($insert_post_log["post_id"],$this->uid);
		$return = array();
		$return['uid1'] = $insert_post_log['uid1'];
		$return['message'] = "发布了一个分享贴";
		$return['uid1_username'] = $userinfo1['username'];
		$return['uid1_head_ico'] = $userinfo1['head_ico'];
		$return['uid1_thread_name'] = $userinfo1['thread_name'];
		$return['uid1_practice_hospital'] = $userinfo1['practice_hospital'];
		$return['uid1_is_expert'] = $userinfo1['is_expert'];
		$return['uid1_verified_status'] = $userinfo1['verified_status'];
		$return['uid1_admin_level'] = $userinfo1['admin_level'];

		$return['post_id'] = $insert_post_log["post_id"];
		$return['post_message'] = $post_info['subject'] ? $post_info['subject'] : $post_info['message'];
		$return['post_imgs'] = $post_info['post_imgs'];
		$return['author_name'] = $post_info['author_name'];
		$return['author_id'] = $post_info['author_id'];
		$return['thread_name'] = $post_info['thread_name'];
		$return['head_ico'] = $post_info['head_ico'];
		$return['admin_level'] = $post_info['admin_level'];
		$return['is_expert'] = $post_info['is_expert'];
		$return['verified_status'] = $post_info['verified_status'];
		$return['aspect_ratio'] = $post_info['aspect_ratio'];
		$return['comment_id'] = $insert_post_log["comment_id"];
		$return['comment_message'] = '';
		$return['action_time'] = $insert_post_log['addtime'];
		$return['action_type'] = $insert_post_log['action_type'];
		$return['favorite_status'] = $post_info['favorite_status'];
		$return['id'] = $insert_post_log['id'];
		$return['post_type_extend'] = $post_info['post_type_extend'];
		return $return;
	}

	/**
	 * 获取动态对应的评论列表
	 */
	public function get_comment_lst_by_event_ids($event_ids)
	{
		if (empty($event_ids)) {
			return array();
		}

		$this->db->where_in("event_id", $event_ids);
		$this->db->where("is_show", "1");
		$records = $this->db->order_by("create_time", "desc")->get('ml_user_event_comment')->result_array();
		$result = array();
		foreach ($records as &$rec) {
			$user_id = $rec['author_id'];
			if (!$user_id) {
				continue;
			}

			$user_info = $this->userm->userm->getUserInfoById($user_id);
			$username = $user_info['username'];
			$rec['username'] = $username;
			$rec['head_ico'] = $user_info['head_ico'];

			$is_replay = $rec['is_replay'];
			if ($is_replay > 0) {
				$user_info = $this->userm->userm->getUserInfoById($is_replay);
				$username = $user_info['username'];
				$rec['replay_username'] = $username;
			}
			$tmp = isset($result[$rec["event_id"]]) ? $result[$rec["event_id"]] : array();

			array_push($tmp, $rec);

			$result[$rec["event_id"]] = $tmp;
		}
		return $result;
	}

	/**
	 * V2.2 获取用户行文日志 （动态）
	 * @param $user_id
	 * @param $res_name
	 * @param $res_id
	 * @param int $page
	 * @param int $per_page
	 */
	public function get_user_event_list($user_id = 0, $res_name = '', $res_id = 0, $page = 1, $per_page = 20)
	{
		$start = ($page - 1) * $per_page;
		if (!$user_id || !$res_name) return array();

		$user_ids = $this->followm->get_follow_ids($user_id);
		$user_ids = $user_ids ? $user_ids : array(-1);

		$filter = array();
		if ($res_name == 'medical_record') { // 病历
			$filter['a.type'] = self::USEREVENTTYPE_MEDICAL_RECORD;
			$this->db->where_in('a.uid1', $user_ids);
			$this->remove_unread_notice($user_id, 1);
		} elseif ($res_name == 'question_vote') { // 投票
			$filter['a.type'] = self::USEREVENTTYPE_QUESTION_VOTE;
			$this->db->where_in('a.uid1', $user_ids);
			$this->remove_unread_notice($user_id, 2);
		} elseif ($res_name == 'other_user') { // 他人
			$filter['a.uid1'] = $res_id;
		} else { // 我的
			$filter['a.uid1'] = $user_id;
		}

		$time = strtotime("-1 month");

		// 优化SQL
		$result = $this->db->select('a.*')
				->from('ml_user_event_log as a')
				->where('a.addtime >=', $time)
				->where($filter)
				->group_start()
				->where('b.is_show', 1)
				->or_where('a.post_id', 0)
				->group_end()
				->group_start()
				->where('c.is_show', 1)
				->or_where('a.comment_id', 0)
				->group_end()
				->join('ml_post as b', 'a.post_id = b.id', 'left')
				->join('ml_comment as c', 'a.comment_id = c.id', 'left')
				->order_by('a.id', 'DESC')
				->limit($per_page, $start)
				->get()
				->result_array();


		if (!$result) return array();

		// 加载用户 event 列表
		list($user_event_log_return_array, $event_ids) = $this->get_user_event_by_log_results($result, $user_id);
		return $user_event_log_return_array;
	}

	//获取关注信息

	/**
	 * 去除动态未读提示
	 * @param $user_id
	 * @param int $notice_type
	 * @return bool
	 */
	public function remove_unread_notice($user_id, $notice_type = 1)
	{
		$key = USER_INFO_PREFIX . $user_id;
		if ($notice_type == 1) {
			$this->redis->hSet($key, 'medical_status', 0);
		} else {
			$this->redis->hSet($key, 'question_vote_status', 0);
		}
		$this->redis->expire($key, USER_INFO_KEY_EXPIRE_TIME);
		return true;
	}

	public function get_other_user_event_log($observed_id, $passby_id, $page, $page_size)
	{
		/*
                //首先获取用户关注的人数
                $query = $this->cimongo->select(['followers'])->where(['following' => $observed_id])->get('member_relation')->result_array();
                foreach($query AS $value)
                {
                    $data[] = $value['followers'];
                }
                //$my_follow = implode(",", $data);
                //获取用户行为日志  1是发帖 2是评论 3是收藏  4是关注 5是达人计划
                if(!$data || count($data) == 0)
                {
                    return array();
                }
        */
		$start = ($page - 1) * $page_size;
		$lmt = ($start ? $start . ', ' : '') . $page_size;

		if (version_compare($this->softversion, '2.0.0') >= 0) {
			$sql = <<<XXX
SELECT ml_user_event_log.id, ml_user_event_log.action_type, ml_user_event_log.uid1, ml_user_event_log.uid2,
	ml_user_event_log.post_id,ml_user_event_log.comment_id, ml_user_event_log.feed_id, ml_user_event_log.addtime
  from ml_user_event_log
  LEFT JOIN ml_post ON ml_user_event_log.post_id=ml_post.id
  LEFT JOIN ml_comment ON ml_user_event_log.comment_id=ml_comment.id
  LEFT JOIN ml_feed ON ml_user_event_log.feed_id=ml_feed.id
  WHERE  (ml_post.is_show=1 or ml_user_event_log.post_id=0)
    AND (ml_comment.is_show=1 or ml_user_event_log.comment_id=0)
    AND (ml_feed.is_show=1 or ml_user_event_log.feed_id=0)
    AND ml_user_event_log.uid1 = $observed_id
    AND ml_post.post_type_extend in (1,2,3)
  order by ml_user_event_log.addtime desc
  limit $lmt
XXX;

		} else {
			$sql = <<<XXX
SELECT ml_user_event_log.id, ml_user_event_log.action_type, ml_user_event_log.uid1, ml_user_event_log.uid2,
    ml_user_event_log.post_id,ml_user_event_log.comment_id,ml_user_event_log.addtime
  from ml_user_event_log
  LEFT JOIN ml_post ON ml_user_event_log.post_id=ml_post.id
  LEFT JOIN ml_comment ON ml_user_event_log.comment_id=ml_comment.id
  WHERE  (ml_post.is_show=1 or ml_user_event_log.post_id=0)
    AND (ml_comment.is_show=1 or ml_user_event_log.comment_id=0)
    AND ml_user_event_log.uid1 = $observed_id
    and ml_user_event_log.action_type < 5
    AND ml_post.post_type_extend in (1,2,3)
  order by ml_user_event_log.addtime desc
  limit $lmt
XXX;
		}
		$user_event_log_result = $this->db->query($sql)->result_array();

		// 加载用户 event 列表
		list($user_event_log_return_array, $event_ids) = $this->get_user_event_by_log_results($user_event_log_result);

		// 加载点赞列表
		$praise_lst = $this->praisem->get_praise_lst_by_event_ids($event_ids);

		// 加载评论列表
		$comment_lst = $this->get_comment_lst_by_event_ids($event_ids);

		// 加载点赞和评论进结果列表
		foreach ($user_event_log_return_array as &$item) {
			$event_id = $item['id'];
			if (isset($comment_lst[$event_id])) {
				$item['event_comment_lst'] = $comment_lst[$event_id];
			} else {
				$item['event_comment_lst'] = array();
			}
			if (isset($praise_lst[$event_id])) {
				$item['praise_lst'] = $praise_lst[$event_id];
			} else {
				$item['praise_lst'] = array();
			}
		}
//        w_log('$user_event_log_return_array'.json_encode($user_event_log_return_array));
		return $user_event_log_return_array;

	}
    //删除某条动态

	/**
	 * 获取 发布求助 信息
	 * @param $insert_post_log
	 * @return array
	 */
	public function get_add_help_info($insert_post_log)
	{
		$userinfo1 = $this->userm->getUserInfo($insert_post_log['uid1']);
		$post_info = $this->postm->get_one_post_4_search($insert_post_log["post_id"], $this->uid);
		$return = array();
		$return['uid1'] = $insert_post_log['uid1'];
		$return['message'] = "发布了一个求助贴";
		$return['uid1_username'] = $userinfo1['username'];
		$return['uid1_head_ico'] = $userinfo1['head_ico'];
		$return['uid1_thread_name'] = $userinfo1['thread_name'];
		$return['uid1_practice_hospital'] = $userinfo1['practice_hospital'];
		$return['uid1_is_expert'] = $userinfo1['is_expert'];
		$return['uid1_verified_status'] = $userinfo1['verified_status'];
		$return['uid1_admin_level'] = $userinfo1['admin_level'];

		$return['post_id'] = $insert_post_log["post_id"];
		$return['post_message'] = $post_info['subject'] ? $post_info['subject'] : $post_info['message'];
		$return['post_imgs'] = $post_info['post_imgs'];
		$return['author_name'] = $post_info['author_name'];
		$return['author_id'] = $post_info['author_id'];
		$return['thread_name'] = $post_info['thread_name'];
		$return['head_ico'] = $post_info['head_ico'];
		$return['admin_level'] = $post_info['admin_level'];
		$return['is_expert'] = $post_info['is_expert'];
		$return['verified_status'] = $post_info['verified_status'];
		$return['aspect_ratio'] = $post_info['aspect_ratio'];
		$return['comment_id'] = $insert_post_log["comment_id"];
		$return['comment_message'] = '';
		$return['action_time'] = $insert_post_log['addtime'];
		$return['action_type'] = $insert_post_log['action_type'];
		$return['favorite_status'] = $post_info['favorite_status'];
		$return['id'] = $insert_post_log['id'];
		$return['post_type_extend'] = $post_info['post_type_extend'];
		return $return;
	}
	//获取图片高宽

	function praise_user_list($praise_uids)
	{
		$priase_list = array();
		foreach ($praise_uids as $p_uid) {
			$user_info = $this->userm->getUserInfoById($p_uid);
			$username = $user_info['username'];
			$one_user = array('username' => $username, 'user_id' => $p_uid);
			$priase_list[] = $one_user;
		}
		return $priase_list;
	}

	public function del_event_log($event_log_array)
	{
		$return = $this->db->delete('ml_user_event_log', $event_log_array);
		return $return;
	}

	//隐藏评论

	public function get_img_wh($img)
	{
		$info = getimagesize($img);
		$imgwh = array();
		$imgwh['width'] = $info[0];
		$imgwh['height'] = $info[1];
		return $imgwh;
	}

	/**
	 * 插入评论
	 */
	public function insert_comment($author_id, $event_id, $is_replay, $comment_cont)
	{
//		if (empty($is_replay)) {
//			$is_replay = 0;
//		} else {
//			$is_replay = 1;
//		}
		$query = array("author_id" => $author_id, "event_id" => $event_id, "is_replay" => $is_replay,
				"content" => $comment_cont, "create_time" => time());
		$return = $this->db->insert("ml_user_event_comment", $query);
		self::send_dongtai_comment($author_id, $event_id, $is_replay);
		return $this->db->insert_id();
	}

	/**
	 * 评论动态,发送通知消息
	 * @param $user_id
	 * @param $event_id
	 * @return bool
	 */
	public function send_dongtai_comment($user_id, $event_id, $is_replay = 0)
	{
		if (!$event_id) {
			return false;
		}
		$event_info = $this->usereventlogm->get_user_event_detail($event_id);
		if (empty($event_info)) {
			return false;
		}

		$user_info = $this->userm->getUserInfoById($user_id);
		if ($is_replay) {
			$title = $user_info["username"] . "回复了你的评论";
			$to_user_id = $is_replay;
		} else {
			$title = $user_info["username"] . "评论了你的动态";
			$to_user_id = $event_info["uid1"];
		}

		$this->load->model('messagem');
		$res = $this->messagem->snd_dongtai($user_info['id'], $to_user_id, $event_id, $title, $event_info["message"]);
		return true;
	}

	public function get_user_event_detail($event_id)
	{
		$sql = <<<XXX
SELECT ml_user_event_log.id, ml_user_event_log.action_type, ml_user_event_log.uid1, ml_user_event_log.uid2,ml_user_event_log.post_id,
    ml_user_event_log.comment_id, ml_user_event_log.feed_id, ml_user_event_log.addtime
  from ml_user_event_log
  LEFT JOIN ml_post ON ml_user_event_log.post_id=ml_post.id
  LEFT JOIN ml_comment ON ml_user_event_log.comment_id=ml_comment.id
  WHERE  (ml_post.is_show=1 or ml_user_event_log.post_id=0)
    AND (ml_comment.is_show=1 or ml_user_event_log.comment_id=0)
    AND ml_user_event_log.id = $event_id
  order by ml_user_event_log.addtime desc
XXX;

		$user_event_log_result = $this->db->query($sql)->result_array();

		if (empty($user_event_log_result)) {
			return array();
		}
//		print_r($user_event_log_result);exit();

		// 加载用户 event 列表
		list($user_event_log_return_array, $event_ids) = $this->get_user_event_by_log_results($user_event_log_result);

		if (empty($user_event_log_return_array)) {
			return array();
		}


		// 加载点赞列表
		$praise_lst = $this->praisem->get_praise_lst_by_event_ids($event_ids);

		// 加载评论列表
		$comment_lst = $this->get_comment_lst_by_event_ids($event_ids);

		// 加载点赞和评论进结果列表
		$result = $user_event_log_return_array[0];
		if (isset($comment_lst[$event_id])) {
			$result['event_comment_lst'] = $comment_lst[$event_id];
		} else {
			$result['event_comment_lst'] = array();
		}
		if (isset($praise_lst[$event_id])) {
			$result['praise_lst'] = $praise_lst[$event_id];
		} else {
			$result['praise_lst'] = array();
		}
		return $result;
	}

	public function del_comment($event_cid)
	{
		$this->db->set(array("is_show" => 0));
		$this->db->where("event_cid", $event_cid);
		$result = $this->db->update("ml_user_event_comment");
		return $result;
	}

	public function send_dongtai_notice($author_id, $event_id, $is_replay, $comment_cont)
	{
		$event_info = $this->get_user_event_db_record($event_id);
		if (empty($event_info)) {
			return false;
		}

		$this->load->model('messagem');
		if ($is_replay) {
			// 推送回复消息

			if ($author_id != $is_replay) {    // 自己回复自己, 不发
				if (!isset($user_info)) {
					$user_info = $this->userm->getUserInfoById($author_id);
				}
				$title = $user_info["username"] . "在动态中回复了你";
				$this->messagem->snd_dongtai($author_id, $is_replay, $event_id, $title, $comment_cont);
			}
		}


		if ($author_id != $event_info["uid1"] && // 动态主人评论自己,不发
				$event_info["uid1"] != $is_replay    // 被评论人是动态主人,已走回复,不发
		) {
			// 推送评论消息

			if (!isset($user_info)) {
				$user_info = $this->userm->getUserInfoById($author_id);
			}
			$title = $user_info["username"] . "评论了你的动态";
			$this->messagem->snd_dongtai($author_id, $event_info["uid1"], $event_id, $title, $comment_cont);
		}

		return true;
	}

	public function get_user_event_db_record($event_id) {
		$record = $this->db->select()->where('event_id', $event_id)->get('ml_user_event_log')->result_array();
		if (!empty($record)) {
			return $record[0];
		}
		return array();
	}

	public function get_comment_detail_lst_by_event_id($event_id)
	{
		if (!$event_id) {
			return array();
		}

		$this->db->where("event_id", $event_id);
		$this->db->where("is_show", "1");
		$records = $this->db->order_by("create_time", "desc")->get('ml_user_event_comment')->result_array();
		$result = array();
		foreach ($records as &$rec) {
			$user_id = $rec['author_id'];
			if (!$user_id) {
				continue;
			}

			$user_info = $this->userm->userm->getUserInfoById($user_id);
			$username = $user_info['username'];
			$rec['username'] = $username;
			$rec['head_ico'] = $user_info['head_ico'];

			$is_replay = $rec['is_replay'];
			if ($is_replay > 0) {
				$user_info = $this->userm->userm->getUserInfoById($is_replay);
				$username = $user_info['username'];
				$rec['replay_username'] = $username;
			}

			array_push($result, $rec);
		}
		return $result;
	}

	public function get_action_by_feed_id($feed_id) {
		$record = $this->db->select()->where('feed_id', $feed_id)->get('ml_user_event_log')->result_array();
		if (!empty($record)) {
			return $record[0];
		}
		return array();
	}

	public function get_action_num($post_type_extend)
	{
		$arr = array();
		/*if ($post_type_extend == 1)
        {
            $arr['action_type'] = Usereventlogm::USEREVENTACTIONTYPE_ADDSHARE;
            $arr['action'] = 'insert share';
            $arr['type'] = Usereventlogm::USEREVENTTYPE_MEDICAL_RECORD;
        }
        if ($post_type_extend == 2 || $post_type_extend == 3)
        {
            $arr['action_type'] = Usereventlogm::USEREVENTACTIONTYPE_ADDHELP;
            $arr['action'] = 'insert help';
            $arr['type'] = Usereventlogm::USEREVENTTYPE_MEDICAL_RECORD;
        }*/
		if ($post_type_extend == 4) {
			$arr['action_type'] = Usereventlogm::USEREVENTACTIONTYPE_ADD_QUESTION;
			$arr['action'] = 'insert_question';
			$arr['type'] = Usereventlogm::USEREVENTTYPE_QUESTION_VOTE;
		} elseif ($post_type_extend == 5) {
			$arr['action_type'] = Usereventlogm::USEREVENTACTIONTYPE_ADD_VOTE;
			$arr['action'] = 'insert_vote';
			$arr['type'] = Usereventlogm::USEREVENTTYPE_QUESTION_VOTE;
		} else {
			$arr['action_type'] = Usereventlogm::USEREVENTACTIONTYPE_POST;
			$arr['action'] = 'insert_post';
			$arr['type'] = Usereventlogm::USEREVENTTYPE_MEDICAL_RECORD;
		}
		return $arr;
	}

	public function get_action_repect($post_type_extend)
	{
		$arr = array();
//        if ($post_type_extend == 1)
//        {
//            $arr['action_type'] = Usereventlogm::USEREVENTACTIONTYPE_COMMENTSHARE;
//            $arr['action'] = 'comment share';
//            $arr['type'] = Usereventlogm::USEREVENTTYPE_MEDICAL_RECORD;
//        }
//        if ($post_type_extend == 2 || $post_type_extend == 3)
//        {
//            $arr['action_type'] = Usereventlogm::USEREVENTACTIONTYPE_COMMENTHELP;
//            $arr['action'] = 'comment help';
//            $arr['type'] = Usereventlogm::USEREVENTTYPE_MEDICAL_RECORD;
//        }
		if ($post_type_extend == 4) {
			$arr['action_type'] = Usereventlogm::USEREVENTACTIONTYPE_COMMENT_QUESTION;
			$arr['action'] = 'comment_question';
			$arr['type'] = Usereventlogm::USEREVENTTYPE_QUESTION_VOTE;
		} else if ($post_type_extend == 5) {
			$arr['action_type'] = Usereventlogm::USEREVENTACTIONTYPE_COMMENT_VOTE;
			$arr['action'] = 'comment_vote';
			$arr['type'] = Usereventlogm::USEREVENTTYPE_QUESTION_VOTE;
		} else {
			$arr['action_type'] = Usereventlogm::USEREVENTACTIONTYPE_COMMENT;
			$arr['action'] = 'insert_comment';
			$arr['type'] = Usereventlogm::USEREVENTTYPE_MEDICAL_RECORD;
		}
		return $arr;
	}

	/**
	 * 递归 遍历拼接用户动态数据
	 * @param $data
	 * @param $i
	 * @param $user_id
	 * @return mixed
	 */
	public function get_continuous_i($data, $i, $user_id) {
		if (isset($data[$i-1]) && $data[$i-1]["action_type"] == Usereventlogm::USEREVENTACTIONTYPE_FOLLOW && $data[$i-1]["uid1"] == $user_id) {
			return $this->get_continuous_i($data, $i-1, $user_id);
		} else {
			return $i;
		}
	}
}
